package org.ucas.kgraph.dto;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Defaults;
import org.springframework.http.HttpStatus;
import org.ucas.kgraph.entity.Schem;
import org.ucas.kgraph.utils.exception.NotFoundException;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class SchemDto  {

  public Long id;

  public Long account_id;

  public String content;

  public String root;
  
  public String type;
  
  public String state;
  
  public String theme;
  
  public static SchemDto ofroot(Schem schem){
    if(schem==null) throw NotFoundException.Term_NotExists;
    SchemDto schemDto = new SchemDto();
    schemDto.id = schem.getId();
    schemDto.account_id = schem.getAccount_id();
    schemDto.content = null;
    schemDto.root=schem.getRoot();
    schemDto.type=schem.getType();
    schemDto.state=schem.getState();
    return schemDto;
  }
  
  public static SchemDto of(Schem schem){
    if(schem==null) throw NotFoundException.Term_NotExists;
    SchemDto schemDto = new SchemDto();
    schemDto.id = schem.getId();
    schemDto.account_id = schem.getAccount_id();
    schemDto.content = schem.getContent();
    schemDto.root=schem.getRoot();
    schemDto.type=schem.getType();
    schemDto.state=schem.getState();
    schemDto.theme=schem.getTheme();
    return schemDto;
  }

  public static SchemDto from(HttpServletRequest request){
    SchemDto schemDto = new SchemDto();
    schemDto.id = request.getParameter("id")==null?null:Long.valueOf(request.getParameter("id"));
    schemDto.account_id = Long.valueOf(request.getParameter("account_id"));
    schemDto.content = request.getParameter("content");
    schemDto.root = request.getParameter("root");
    schemDto.type = request.getParameter("type");
    schemDto.state = request.getParameter("state");
    schemDto.theme = request.getParameter("theme");
    return schemDto;
  }

  /**
   * 更新Term,但核心信息如定义和Id及RootId只允许Term中为空也就是创建时才可以写入
   * level等属性只允许计算得到,不允许手动写入
   * 其他属性只有非空时才会写入
   * @param term
   * @return
   */
  public Schem writeTo(Schem schem){

    if(schem==null) throw NotFoundException.Term_NotExists;

    //仅当term中Id和rootId为空时才允许改动
    if(schem.getId()==null) schem.setId(this.id);
    if(schem.getAccount_id()==null) schem.setAccount_id(this.account_id);
    if(schem.getContent()==null) schem.setContent(this.content);
    if(schem.getRoot()==null) schem.setRoot(this.root);
    if(schem.getType()==null) schem.setType(this.type);
    if(schem.getState()==null) schem.setState(this.state);
    if(schem.getTheme()==null) schem.setTheme(this.theme);
    return schem;
  }

  public Long getId() {
	return id;
  }

  public void setId(Long id) {
	this.id = id;
  }

  public Long getAccount_id() {
	return account_id;
  }

  public void setAccount_id(Long account_id) {
	this.account_id = account_id;
  }

  public String getContent() {
	return content;
  }

  public void setContent(String content) {
	this.content = content;
  }

  public String getRoot() {
	return root;
  }

  public void setRoot(String root) {
	this.root = root;
  }

  public String getType() {
	return type;
  }

  public void setType(String type) {
	this.type = type;
  }

  public String getState() {
	return state;
  }

  public void setState(String state) {
	this.state = state;
  }
  
}
